<?php
include_once 'paomeBase.class.php';

/**
 * 大厅相关
 */
class paomeHallModel extends paomeBaseModel
{

    public function __construct(& $base)
    {
        parent::__construct($base);

        $this->table = 'r_pm_hall_recommend';
    }

    /*
     * 获取当前所处赛季
     */
    public function getCurrentSeason(){
        $season = $this->paomeDb->getRow("SELECT * FROM r_pm_contest WHERE flag = 1");
        return $season;
    }

    /*
     * 跑么杯--获取当前时间所处轮次
     */
    public function getCurrentRound(){
        $season = $this->getCurrentSeason();
        $roundsDict = common::$roundsDict;
        $round = 0;
        if($season){
            foreach ($roundsDict as $k => $v) {
                if(in_array($season['status'], $v)){
                    $round = $k;
                }
            }
        }        
        return $round;
    }

    /*
     * 获取正在推荐的俱乐部
     */
    public function getclub(){
        $data = $this->redis->hgetall(REDIS_PREFIX.'clubRecommend');
        if(!empty($data)){
            foreach ($data as &$v) {
                $v = json_decode($v, true);
                !empty($v['start_time']) && $v['start_time'] = date('Y-m-d H:i:s',$v['start_time']);
                !empty($v['end_time']) && $v['end_time'] = date('Y-m-d H:i:s',$v['end_time']);
            }
        }
        return $data;
    }

    /*
     * 解散俱乐部时推荐的俱乐部下线
     */
    public function delClubRecomment($clubId){
        //查询该俱乐部是否正在推荐
        $sql = "select * from r_pm_hall_recommend where club_id = {$clubId} and sort != 0";
        $clubInfo = $this->paomeDb->getRow($sql);
        if($clubInfo){
            $this->redis->hdel(REDIS_PREFIX.'clubRecommend', $clubId);
            $this->paomeDb->update("update r_pm_hall_recommend set sort = 0 where club_id = {$clubId}");
        }
    }

    /*
     * 获取正在推荐的用户
     */
    public function getuser(){
        $data = $this->redis->hgetall(REDIS_PREFIX.'userRecommend');
        if(!empty($data)){
            foreach ($data as &$v) {
                $v = json_decode($v, true);
                !empty($v['start_time']) && $v['start_time'] = date('Y-m-d H:i:s',$v['start_time']);
                !empty($v['end_time']) && $v['end_time'] = date('Y-m-d H:i:s',$v['end_time']);
            }
        }
        return $data;
    }

    /*
     * 根据时间获取所处赛季
     * start_time  开始时间
     * end_time    结束时间
     */
    public function getSeasonByTime($start_time, $end_time){
        $sql = "select * from r_pm_contest where 1=1";
        !empty($start_time) && $sql .= " and sign_time <= {$start_time}";
        !empty($end_time) && $sql .= " and end_time >= {$end_time}";
        $season = $this->paomeDb->getRow($sql);

        return $season;
    }

    /*
     * 根据条件获取所处阶段
     * start_time  开始时间
     * end_time    结束时间
     */
    public function getPhaseByTime($start_time, $end_time){
        $sql = "select * from r_pm_rounds where 1=1";
        !empty($start_time) && $sql .= " and start_time <= {$start_time}";
        !empty($end_time) && $sql .= " and end_time >= {$end_time}";
        $phase = $this->paomeDb->getRow($sql);
        
        return $phase;
    }

    /*
     * 根据赛季获取所有轮次
     * contest_id  赛季id
     */
    public function getPhaseBySeason($contest_id){
        $sql = "select * from r_pm_rounds where 1=1";
        !empty($contest_id) && $sql .= " and contest_id = {$contest_id}";
        $data = $this->paomeDb->getAll($sql);

        return $data;
    }

    /*
     * 获取成员所属俱乐部
     */
    public function getClubByUid($uid){
        $sql = "SELECT * FROM r_pm_club_users c WHERE c.STATUS = 0 and c.uid = {$uid}";
        $data = $this->paomeDb->getRow($sql);  
        $clubInfo = array();     
         if($data){
            $clubSql = "SELECT * FROM r_pm_club c WHERE c. STATUS = 0 and c.id = {$data['club_id']}";
            $clubInfo = $this->paomeDb->getRow($clubSql);
         }
        return $clubInfo;
    }

    /*
     * 获取正能量土豪榜(前十名)
     */
    public function getHallEnergyList(){
        $season = $this->getCurrentSeason();
        $data = array();
        if($season){
            $seasonId = $season['id'];
            $round = $this->getCurrentRound();        
            if(!empty($round)){
                $sql = "SELECT *, SUM(energy) total_energy FROM r_pm_supporters e WHERE e.type = 1 AND e.round = {$round} AND contest_id = {$seasonId} GROUP BY e.uid ORDER BY total_energy DESC LIMIT 10";
                $data = $this->paomeDb->getAll($sql);
            }
        }
        if($data){
            foreach ($data as &$v) {
                $userInfo = $this->redis->hgetall(REDIS_PREFIX . 'UserInfo_' . $v['uid']);
                if($userInfo){
                    $v['nickname'] = $userInfo['nickname'];
                    $v['header'] = $userInfo['header'];
                }
            }
         }
        return $data;
    }

    /*
     * 名人堂排行榜(前十名)
     * $params  $type   维度
     * $params  $order  排序
     */
    public function getHallOfFameList($type, $order = 'DESC'){
        $season = $this->getCurrentSeason();
        $data = array();
        if($season){
            $seasonId = $season['id'];
            $round = $this->getCurrentRound();   
            if(!empty($round)){
                $sql = "SELECT * FROM r_pm_users_rounds r WHERE r.type = {$round} AND contest_id = {$seasonId} AND {$type} > 0 ORDER BY {$type} {$order} LIMIT 10";
                $data = $this->paomeDb->getAll($sql);
            }
            if($data){
                foreach ($data as &$v) {
                    $userInfo = $this->redis->hgetall(REDIS_PREFIX . 'UserInfo_' . $v['uid']);
                    $v['nickname'] = '';
                    $v['header'] = '';
                    if($userInfo){
                        $v['nickname'] = $userInfo['nickname'];
                        $v['header'] = $userInfo['header'];
                    }
                    $v['speed'] = common::sec3format($v['speed']);
                }
             }
        }
        return $data;
    }

    /*
     * 获取需要排名的总人数
     */
    public function getNeedRankUser(){
        $season = $this->getCurrentSeason();
        $data = 0;
        if($season){
            $seasonId = $season['id'];
            $round = $this->getCurrentRound();   
            if(!empty($round)){
                $sql = "SELECT count(1) FROM r_pm_users_rounds r WHERE r.type = {$round} AND contest_id = {$seasonId}";
                $data = $this->paomeDb->getOne($sql);
            }
        }
        return $data;
    }

    /*
     * 解析大厅资讯相关内容(最新一条的帖子标题)
     */
    public function getTopicInfo($topicName){
        $sql = "SELECT topic_id FROM r_topic WHERE name = \"{$topicName}\" AND status != 1";
        $topic_id = $this->paomeDb->getOne($sql);
        $title = '';
        if($topic_id){
            $sql = "SELECT short_desc FROM r_news WHERE news_id in (select news_id from r_topic_relation where topic_id = {$topic_id}) AND is_show = 1 order by lastupdate desc";
            $title = $this->paomeDb->getOne($sql);
        }
        return $title;
    }

    /*
     * 根据帖子id获取帖子名称
     */
    public function getNewsName($newsId){
        $sql = "SELECT short_desc FROM r_news WHERE news_id = {$newsId}";
        return $this->paomeDb->getOne($sql);
    }
}